package me.zhengjie.modules.website.repository;

import me.zhengjie.modules.website.domain.ArchiveInformation;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 * @author system
 * @since 2024-11-05 23:38:48
 */
@Component
public class ArchiveInformationSpecifications {

    public static Specification<ArchiveInformation> queryList(ArchiveInformation archiveInformation) {
        return (root, query, builder) -> {
            List<Predicate> predicates = new ArrayList<>();
            if (archiveInformation.getId() != null) {
                predicates.add(builder.equal(root.get("id" ).as(Long.class), archiveInformation.getId()));
            }

            if (archiveInformation.getArchiveNumber() != null) {
                predicates.add(builder.like(root.get("archiveNumber" ).as(String.class), "%" + archiveInformation.getArchiveNumber() + "%" ));
            }
            if (archiveInformation.getTitle() != null) {
                predicates.add(builder.like(root.get("title" ).as(String.class), "%" + archiveInformation.getTitle() + "%" ));
            }
            if (archiveInformation.getDocumentTime() != null) {
                predicates.add(builder.like(root.get("documentTime" ).as(String.class), "%" + archiveInformation.getDocumentTime() + "%" ));
            }
            Predicate[] p = new Predicate[predicates.size()];
            return query.where(builder.and(predicates.toArray(p))).getRestriction();
        };
    }

}
